package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.GoodsPriceMapper;
import com.gxa.zhonglong.pojo.GoodsPrice;
import com.gxa.zhonglong.service.GoodsListService;
import com.gxa.zhonglong.service.GoodsPriceService;
import com.gxa.zhonglong.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author NITRO
 */

@Service("goodsPriceService")
@Transactional(rollbackFor = {Throwable.class})
public class GoodsPriceServiceImpl implements GoodsPriceService {

    @Autowired
   private GoodsPriceMapper goodsPriceMapper;

    @Override
    public Boolean addGoodsPrice(GoodsPrice goodsPrice) throws BusinessException {
        GoodsPrice goodsPrice1 = goodsPriceMapper.selectGoodsPriceByCode(goodsPrice.getCode());
        System.out.println(goodsPrice1);
        if(goodsPrice1==null){
            goodsPriceMapper.addPrice(goodsPrice);
            return true;
        }else{
            throw new BusinessException(ExceptionCode.ADD_DEFEATED);
        }
    }

    @Override
    public void deleteGoodsPrice(List<Long> ids) throws BusinessException {
        if (goodsPriceMapper.deletePrice(ids)<1) {
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }
    }

    @Override
    public Boolean updateGoodsPrice(GoodsPrice goodsPrice) throws BusinessException {
        Boolean aBoolean = goodsPriceMapper.updatePrice(goodsPrice);
        if(aBoolean){
            return true;
        }else{
            throw  new BusinessException(ExceptionCode.UPDATE_FALSE);
        }
    }

    @Override
    public List<GoodsPrice> getGoodsPriceList() {
        List<GoodsPrice> goodsPrices = goodsPriceMapper.selectGoodsPrice();
        return goodsPrices;
    }
}
